import Vue from 'vue'
import VueRouter from 'vue-router'

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    redirect:'/home',//重定向
    children:[
      {path: 'home',name: 'Home',meta:{name:'订单管理'},component: () => import('../views/Manager/Home.vue')},
      {path: 'user',name: 'User',meta:{name:'顾客信息'},component: () => import('../views/Manager/User.vue')},
      {path: 'book',name: 'Book',meta:{name:'书籍信息'},component: () => import('../views/Manager/Book.vue')},
      {path: 'vip',name: 'Vip',meta:{name:'会员信息'},component: () => import('../views/Manager/Vip.vue')},
      {path: 'clerk',name: 'Clerk',meta:{name:'店员信息'},component: () => import('../views/Manager/Clerk.vue')},
      {path: 'publisher',name: 'Publisher',meta:{name:'供应商信息'},component: () => import('../views/Manager/Publisher.vue')},
      {path: 'activity',name: 'Activity',meta:{name:'活动板块'},component: () => import('../views/Manager/Activity.vue')},
      {path: 'idea',name: 'Idea',meta:{name:'理念板块'},component: () => import('../views/Manager/Idea.vue')},
      {path: 'recommend',name: 'Recommend',meta:{name:'推荐板块'},component: () => import('../views/Manager/Recommend.vue')},
      {path: 'discount',name: 'Discount',meta:{name:'折扣板块'},component: () => import('../views/Manager/Discount.vue')},
      {path: 'buy',name: 'Buy',meta:{name:'购书管理'},component: () => import('../views/Manager/Buy.vue')},
      {path: 'sell',name: 'Sell',meta:{name:'售书管理'},component: () => import('../views/Manager/Sell.vue')},
      {path: 'recharge',name: 'Recharge',meta:{name:'会员充值'},component: () => import('../views/Manager/Recharge.vue')},
      {path: 'profit',name: 'Profit',meta:{name:'利润分析'},component: () => import('../views/Manager/Profit.vue')},
      {path: 'person',name: 'Person',meta:{name:'个人信息'},component: () => import('../views/Manager/Person.vue')},
      {path: 'password',name: 'Password',meta:{name:'修改密码'},component: () => import('../views/Manager/Password.vue')},
    ]
  },
  {
    path:'/login',
    name:'login',
    component: () => import('../views/Login.vue')
  },
  {
    path:'/registry',
    name:'registry',
    component: () => import('../views/Registry.vue')
  },
  {
    path:'/403',
    name:'auth',
    meta:{name:'无权限'},
    component: () => import('../views/Auth.vue')
  },
  {
    path:'*',
    name:'404',
    meta:{name:'无法访问'},
    component: () => import('../views/404.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  let adminPaths=['/user','/publisher','/vip','/clerk','/profit']
  let user=JSON.parse(localStorage.getItem('honey-user') || '{}')
  if(user.role!=='店长'&&adminPaths.includes(to.path)){
    next('/403')
  }else{
    next()
  }
})

export default router
